;$(function(){
	// 1.实现切换不同的用户登录方式
	function tabChange(obj1,obj2){
		$(obj1).on("click",".right-h",function(){
			$(".right-h").parent().parent().css("display","none")
			$(".right-h").parent().parent().siblings(obj2).css("display","block");
	    })
	}
	tabChange(".accountnum-log",".mobile-log");
	tabChange(".mobile-log",".accountnum-log");

// 2.实现图片验证码的切换(利用跨域jsonp)
	ucaptchaTab();
	$("#ucaptcha").on("click",ucaptchaTab);
	function ucaptchaTab(){
		$.ajax({
			type:"GET",
			url:"http://route.showapi.com/26-4?showapi_appid=34271&showapi_sign=43c8a20e560e4936a97e16d49fc3fdbb",
			dataType:"json",
			success:function(data){
				$("#ucaptcha").attr("src",data.showapi_res_body.img_path);
				var $msg = data.showapi_res_body.text;
			}
		})
	}
// 3.实现表单验证
	
	$.validator.addMethod("phone",function(value,element){
        var reg = /^1[0-9]{10}$/;
        // console.log(element)
        return this.optional(element)||value.match(reg);
    },"手机号不能为空");
    $.validator.addMethod("captcha",function(value,element){
    	var reg = /^[0-9]{6}$/;
    	return this.optional(element)||value.match(reg);
    },"验证码不能为空")

	$("#accountnum-log").validate({
		errorElement:"b",
		errorPlacement:function(error,element){
            error.appendTo(element.parent());
        },
		rules:{
			phone:{
				required:true,
				phone:true
			},
			captcha:{
				required:true,
				captcha:true
			}
		},
		messages:{
			phone:{
				required:"手机号码不能为空",
				phone:"请填写正确的手机号码"
			},
			captcha:{
				required:"验证码不能为空",
				captcha:"验证码不正确，请重新输入"
			}
		},
		submitHandler:function(){
			$.cookie("phone",$('.pho-num').val(),{expires:7,path:'./html/login-watch.html'});
			
			return false
		}
	})
	$.validator.addMethod('password',function(value,element){
		var reg=/^\w{6,}/
		return this.optional(element)||value.match(reg)
	},'输入格式错误')
	$('#mobile-log').validate({
		errorElement:'b',
		errorPlacement:function(error,element){
			error.appendTo(element.parent())
		},
		rules:{
			username:{
				required:true,
			},
			passwd:{
				required:true,
				password:true
			}
		},
		messages:{
			username:{
				required:'请按要求输入正确格式'
			},
			passwd:{
				required:'密码不能为空',
				password:'不能低于六位'
			}
		},
		submitHandler:function(){
			if($('[name=remember]').prop('checked')){
				$.cookie("username",$('[name=username]').val(),{expires:7,path:'./html/login-watch.html'})
			}else{
				$.cookie("username",$('[name=username]').val(),{expires:-1,path:'./html/login-watch.html'})
			}
			// console.log('a')
			$.ajax({//模拟ajax请求
				type:'GET',
				url:'./php/login.php',
				dataType:'JSON',
				data:{
					username:$('[name=username]').val(),
					password:$('[name=passwd]').val()
				},
				success:function(data){
					// console.log(data)
					if(data.status=='success'){
						location.href='https://www.baidu.com'
					}else{
						$('#error').css('display','block')
					}
				}
			})
			return false;
		}
	})
	$('#error>div i').on('click',function(){
		$('#error').css('display','none')
	})
//登陆cookie
	$(function(){
		if ($.cookie("phone")||$.cookie("username")) {
			$('.pho-num').val($.cookie("phone"));
			$('[name=username]').val($.cookie("username"))
		}
	})
	//获取验证码
	var num=60;
	var timer
	$('.get-captcha').on('click',function(){
		// alert('a')
		$('.get-captcha').val('剩余'+(--num)+'秒').attr('disabled',true);
		timer=setInterval(function(){
			if(num>0){
				$('.get-captcha').val('剩余'+(--num)+'秒').attr('disabled',true);
			}else{
				clearInterval(timer)
				$('.get-captcha').val('获取验证码').attr('disabled',false);
				num=60
			}
		},1000)
		$.ajax({
					type:'GET',
					dataType:'JSON',
					url:'',
					success:function(data){

					}
				})
		
	})
})
